#include "device_base.h"
#include <stdio.h>

device_base::device_base(void)
{
}

device_base::~device_base(void)
{
	_locker.uninit();
	_skf_locker.uninit();
}

int device_base::open()
{
	return -1;
}

int device_base::close()
{
	return -1;
}

int device_base::set_parameter(int param, unsigned char * pValue, int valueLen)
{
	return -1;
}

int device_base::send_data(unsigned char * request, int request_len)
{
	return -1;
}

int device_base::receive_data( unsigned char * response, int *response_len )
{
	return -1;
}

int device_base::device_io( unsigned char * request, int request_len,unsigned char * response, int *response_len )
{
	return -1;
}

int device_base::set_comm_timeout( int time_out )
{
	return -1;
}

char * device_base::get_dev_path()
{
	return 0;
}

int device_base::set_dev_state( enmDevState state )
{
	return 0;
}

enmDevState device_base::get_dev_state()
{
	return devStateAny;
}

char * device_base::get_dev_name()
{
	return 0;
}

void device_base::set_dev_name( char * devName )
{
	return ;
}

int device_base::send_raw_data( unsigned char * cdb, int cbw_len, 
							   unsigned char * request, long request_len )
{
	return -1;
}

int device_base::receive_raw_data( unsigned char * cdb, int cbw_len, 
								  unsigned char * response, long *response_len )
{
	return -1;
}

int device_base::get_device_type()
{
	return 0;
}

int device_base::dev_present()
{
	return 0;
}

HANDLE device_base::get_dev_os_handle()
{
	return 0;
}

int device_base::begin_transaction(long timeOut)
{
	char lockerName[256] = {0};
	sprintf(lockerName, "%s_trans", get_dev_name());

	if(strstr(_skf_locker.get_mutext_name(), lockerName) == NULL) //mutex changed
	{
		_skf_locker.uninit();
	}

	if(0 != _skf_locker.init(lockerName, NULL))
	{
		return 1;
	}

	_skf_locker.lock(timeOut);

	return 0;
}

int device_base::end_transaction()
{
	int lockCount = _skf_locker.unlock();
	return 0;
}

int device_base::lock( long timeOut )
{
	if(strstr(_locker.get_mutext_name(), get_dev_name()) == NULL) //mutex changed
	{
		_locker.uninit();
	}

	if(0 != _locker.init(get_dev_name(), NULL))
	{
		return 1;
	}

	_locker.lock(timeOut);
	return 0;
}

int device_base::lock(long timeOut, char * name)
{
	if(strstr(_locker.get_mutext_name(), name) == NULL) //mutex changed
	{
		_locker.uninit();
	}

	if(0 != _locker.init(name, NULL))
	{
		return 1;
	}

	_locker.lock(timeOut);
	return 0;
}

int device_base::unlock()
{
	int lockCount = _locker.unlock();
	return 0;
}

int device_base::get_hs_apdu_size(int * size)
{
	* size = 0;
	return 0;
}


int device_base::crypt_with_high_speed(unsigned long algo_id, int mode, int appid, int containerId, int keyId,
									   int firstPackage, unsigned char * iv, int iv_len,
									   unsigned char * input, int input_len, unsigned char * output, int *output_len)
{
	return 0;
}

int device_base::enable_high_speed( int mode )
{
	return 0;
}



